package com.olah.user.service;

import com.olah.common.entity.dto.request.user.LoginInfo;
import com.olah.common.entity.dto.request.user.RegisterInfo;
import com.olah.common.entity.dto.request.user.SearchUserInfo;
import com.olah.common.entity.po.user.User;
import com.olah.common.entity.po.user.UserHiddenRoots;
import com.olah.user.dao.BaseDao;
import com.olah.user.dao.RootsDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseService {

    @Resource
    private BaseDao dao;

    @Resource
    private RootsDao rootsDao;

    public Map<String, Object> queryById(Integer id) {
        User user = dao.queryById(id);
        if (user == null) {
            return null;
        }
        List<String> userGoodness = dao.getUserGoodness(user.getId());
        user.setGoodness(userGoodness);
        UserHiddenRoots roots = rootsDao.queryHidden(id);
        user.doHidden(roots);
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("roots", roots);
        return map;
    }

    public List<User> queryByInfo(SearchUserInfo info) {
        List<User> userList = dao.queryByInfo(info);
        userList.forEach(user -> {
            UserHiddenRoots hiddenRoots = rootsDao.queryHidden(user.getId());
            List<String> userGoodness = dao.getUserGoodness(user.getId());
            user.setGoodness(userGoodness);
            user.doHidden(hiddenRoots);
        });
        return userList;
    }
    public void register(RegisterInfo info) {
        dao.register(info);
    }

    public User login(LoginInfo info) {
        User user = dao.login(info);
        if (user != null) {
            List<String> goodness = dao.getUserGoodness(user.getId());
            user.setGoodness(goodness);
        }
        return user;
    }

}
